﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace DAL
{
    public class StudentInfoEntityDal
    {
        readonly private SQLHelper sqlhelper = null;

        public StudentInfoEntityDal()
        {
            sqlhelper = new SQLHelper();
        }

        readonly TBToList<StudentInfoEntity> dtToList = new TBToList<StudentInfoEntity>();

        #region 选择学生信息通过学号---周洲--2015-11-21


        /// <summary>
        /// 选择学生信息通过学号---周洲--2015-11-21
        /// </summary>
        /// <param name="studentinfo"></param>
        /// <returns></returns>
        public DataTable SelectStudentInfoByID(StudentInfoEntity studentinfo)
        {
            string sql = "select * from StudentInfoEntity where studentID=@studentID";
            SqlParameter[] paras = new SqlParameter[]{
                new  SqlParameter("@studentID",studentinfo.studentID )
            };
            DataTable dt = sqlhelper.ExecuteQuery(sql, paras, CommandType.Text);
            return dt;
        }
        #endregion

        #region 选择学生信息通过学号---苑仁杰--2018-12-20
        public DataTable SelectStudentInfoBycode(StudentInfoEntity studentinfo)
        {
            string sql = "select * from StudentInfoEntity where code=@code";
            SqlParameter[] paras = new SqlParameter[]{
                new  SqlParameter("@code",studentinfo.code )
            };
            DataTable dt = sqlhelper.ExecuteQuery(sql, paras, CommandType.Text);
            return dt;
        }
        #endregion

        #region 清空验证码通过学号---苑仁杰--2018-12-20
        public int UpdateStudentInfoBycode(StudentInfoEntity studentinfo)
        {
            string sql = "update StudentInfoEntity set code=null where studentID=@studentID";
            SqlParameter[] paras = new SqlParameter[]{
                new  SqlParameter("@studentID",studentinfo.studentID )
            };
            int flag = sqlhelper.ExecuteNonQuery(sql, paras, CommandType.Text);
            return flag;
        }
        #endregion

        #region 通过studentID选择学生的试卷类型（A,B,C）--周洲--2015年11月21日
        /// <summary>
        /// 通过studentID选择学生的试卷类型（A,B,C）--周洲--2015年11月21日
        /// </summary>
        /// <param name="studentinfo"></param>
        /// <returns></returns>
        public DataTable SelectPaperTypebyStudentID(StudentBindPaperTypeEntity studentinfo)
        {
            string sql = "select * from StudentBindPaperTypeEntity where StudentID=@studentID and TimeTamp=@tamtap";
            SqlParameter[] paras = new SqlParameter[]{
                new  SqlParameter("@studentID",studentinfo.StudentID  ),
                 new  SqlParameter("@tamtap",studentinfo.TimeTamp  )
            };
            DataTable dt = sqlhelper.ExecuteQuery(sql, paras, CommandType.Text);
            return dt;

        }
        #endregion


        #region 通过学号 查询学生信息 -赵崇-2015年11月24日 17:01:58
        /// <summary>
        /// 通过学号 查询学生信息
        /// </summary>
        /// <param name="studentId">学号</param>
        /// <returns>学生信息</returns>
        public StudentInfoEntity GetStudentById(string studentId)
        {
            string sql = "select * from StudentInfoEntity where studentID=@studentID";
            SqlParameter[] paras = new SqlParameter[]{
                new  SqlParameter("@studentID",studentId )
            };
            DataTable dt = sqlhelper.ExecuteQuery(sql, paras, CommandType.Text);
            if (dt.Rows.Count != 0)
            {
                //2,把查询结果组织成  实体List
                List<StudentInfoEntity> lstStudent = dtToList.ToList(dt);
                if (lstStudent.Count != 0)
                {
                    return lstStudent[0];
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }


        }
        #endregion
        #region"根据学生的学号查询学生姓名-韩梦甜-2015-12-16"
        /// <summary>
        /// 根据学生的学号查询学生姓名-韩梦甜-2015-12-16
        /// </summary>
        /// <param name="studentid"></param>
        /// <returns></returns>
        public DataTable SelectStudentNameByStudentID(String studentid)
        {
            string cmdText = "select studentID,studentName from StudentInfoEntity where studentID in(" + studentid + ")";
            DataTable dt = sqlhelper.ExecuteQuery(cmdText, CommandType.Text);
            return dt;
        }
        #endregion
        #region"模糊查询学生的学号和姓名-韩梦甜-2015-12-16"
        /// <summary>
        /// 模糊查询学生的学号和姓名-韩梦甜-2015-12-16
        /// </summary>
        /// <param name="studentinfo"></param>
        /// <returns></returns>
        public DataTable SelectVagueStudent(StudentInfoEntity studentinfo)
        {
            string cmdText = "select studentID,studentName from StudentInfoEntity where studentID=@studentID";

            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@studentID", studentinfo.studentID) };

            DataTable dt = sqlhelper.ExecuteQuery(cmdText, paras, CommandType.Text);
            return dt;
        }
        #endregion
        #region"根据学号查询学生所在学院ID-韩梦甜-2015-12-16"
        /// <summary>
        /// 根据学号查询学生所在学院ID-韩梦甜-2015-12-16
        /// </summary>
        /// <param name="studentinfo"></param>
        /// <returns></returns>
        public DataTable SelectCollegeIDByStudentID(StudentInfoEntity studentinfo)
        {
            string cmdText = "select CollegeID from StudentInfoEntity where studentID=@studentID";

            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@studentID", studentinfo.studentID) };

            DataTable dt = sqlhelper.ExecuteQuery(cmdText, paras, CommandType.Text);
            return dt;
        }
        #endregion
    }
}
